package cn.edu.xust.service.impl;


import cn.edu.xust.dao.SysMenuprivilegeDao;
import cn.edu.xust.entity.SysMenuEntity;
import cn.edu.xust.entity.SysMenuprivilegeEntity;
import cn.edu.xust.service.SysMenuService;
import cn.edu.xust.service.SysMenuprivilegeService;
import cn.edu.xust.service.SysUserService;
import cn.edu.xust.util.PageUtils;
import cn.edu.xust.util.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("sysMenuprivilegeService")
public class SysMenuprivilegeServiceImpl extends ServiceImpl<SysMenuprivilegeDao, SysMenuprivilegeEntity> implements SysMenuprivilegeService {

    @Autowired
    @Lazy
    SysMenuService sysMenuService;
    @Autowired
    @Lazy
    SysUserService sysUserService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SysMenuprivilegeEntity> page = this.page(
                new Query<SysMenuprivilegeEntity>().getPage(params),
                new QueryWrapper<SysMenuprivilegeEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public SysMenuprivilegeEntity getBymp(String menuid, String pname) {

        QueryWrapper<SysMenuprivilegeEntity> wap = new QueryWrapper<SysMenuprivilegeEntity>().eq("menuid", menuid).eq("privilegename", pname);
        return baseMapper.selectOne(wap);

    }

    @Override
    public List<SysMenuprivilegeEntity> getBypids(List<Integer> preveids) {


        QueryWrapper<SysMenuprivilegeEntity> wap = new QueryWrapper<SysMenuprivilegeEntity>().in("privilegeid", preveids);
        return baseMapper.selectList(wap);
    }

    @Override
    public List<SysMenuprivilegeEntity> getbymenuids(List<Integer> menu2ids) {
        QueryWrapper<SysMenuprivilegeEntity> wap = new QueryWrapper<SysMenuprivilegeEntity>().in("menuid", menu2ids);
        return baseMapper.selectList(wap);

    }

    @Override
    public List<SysMenuprivilegeEntity> getbymenuid(String menuid) {
        QueryWrapper<SysMenuprivilegeEntity> wap = new QueryWrapper<SysMenuprivilegeEntity>().eq("menuid", menuid);
        return baseMapper.selectList(wap);

    }

    @Override
    public List<SysMenuprivilegeEntity> getARankByPids(List<String> prevs2) {

        //拿到menuid 并distinct
        List<Integer> integers = new ArrayList<>();
        for (String s : prevs2) {
            integers.add(Integer.parseInt(s));
        }
        List<SysMenuprivilegeEntity> bypids = this.getBypids(integers);

        //拿到所有二级menuid去重
        List<Integer> collect = bypids.stream().map(SysMenuprivilegeEntity::getMenuid).collect(Collectors.toList());
        List<Integer> collect1 = collect.stream().distinct().collect(Collectors.toList());

        //反查menu全属性
        List<SysMenuEntity> byMenuids = sysMenuService.getByMenuids(collect1);
        //取出二级目录的父级id
        List<Integer> collect2 = byMenuids.stream().map(SysMenuEntity::getFathermid).collect(Collectors.toList());
        //去重
        List<Integer> collect3 = collect2.stream().distinct().collect(Collectors.toList());
        //一级目录查询mp表
        return this.getbymenuids(collect3);


    }

    @Override
    public int removeByMenuids(List<String> menuids) {
        QueryWrapper<SysMenuprivilegeEntity> wap = new QueryWrapper<SysMenuprivilegeEntity>().in("menuid", menuids);
        return baseMapper.delete(wap);
    }


}